package jp.co.htkk.service.impl;

import jp.co.htkk.common.exception.BusinessException;
import jp.co.htkk.dto.${TableName!}Dto;
import jp.co.htkk.dto.UserDto;
import jp.co.htkk.form.${TableName!}RegisterForm;
import jp.co.htkk.repository.${TableName!}Repository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.web.WebAppConfiguration;

import java.io.IOException;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;

@SpringBootTest
@AutoConfigureMockMvc
@WebAppConfiguration
class ${TableName!}ServiceImplTest {
    @Autowired
    private ${TableName!}ServiceImpl ${tableName!}Service;
    @SpyBean
    private ${TableName!}Repository ${tableName!}Repository;
    @Test
    void register() throws IOException {
        ${TableName!}RegisterForm registerForm = new ${TableName!}RegisterForm();
        registerForm.set${TableName!}Id("1");
        registerForm.setUserId("1");
        registerForm.setName("test");
        registerForm.setGender(0);
        registerForm.set${TableName!}birthExpectedDate("20200202");
        registerForm.setBirthDate("20200202");

        doReturn(1).when(${tableName!}Repository).create${TableName!}(any());

        ${TableName!}Dto ${tableName!}Dto = ${tableName!}Service.register(registerForm);
<#list  columns as field>
        assertEquals(registerForm.get${field.ColumnName!}(), ${tableName!}Dto.get${field.ColumnName!}());
</#list>
    }

    @Test
    void get${TableName!}ById() {
        ${TableName!}Dto ${tableName!}Dto = new ${TableName!}Dto();
        ${tableName!}Dto.setId(1l);
        ${tableName!}Dto.set${TableName!}Id("1");
        ${tableName!}Dto.setUserId("1");
        ${tableName!}Dto.setName("test");
        ${tableName!}Dto.setGender(0);
        ${tableName!}Dto.set${TableName!}birthExpectedDate("20200202");
        ${tableName!}Dto.setBirthDate("20200202");
        doReturn(${tableName!}Dto).when(${tableName!}Repository).get${TableName!}ById(any());
        ${TableName!}Dto ${tableName!} = ${tableName!}Service.get${TableName!}ById("1");
        assertNotNull(${tableName!});
    }

    @Test
    void delete${TableName!}ById() {
        ${TableName!}Dto ${tableName!}Dto = new ${TableName!}Dto();
        ${tableName!}Dto.setId(1l);
        ${tableName!}Dto.set${TableName!}Id("1");
        ${tableName!}Dto.setUserId("1");
        ${tableName!}Dto.setName("test");
        ${tableName!}Dto.setGender(0);
        ${tableName!}Dto.set${TableName!}birthExpectedDate("20200202");
        ${tableName!}Dto.setBirthDate("20200202");
        doReturn(1).when(${tableName!}Repository).delete${TableName!}ById(any());
        doReturn(${tableName!}Dto).when(${tableName!}Repository).get${TableName!}ById(any());
        ${TableName!}Dto ${tableName!} = ${tableName!}Service.delete${TableName!}ById("1");
        assertNotNull(${tableName!});
    }

    @Test
    void update${TableName!}() {
        ${TableName!}Dto ${tableName!}Dto = new ${TableName!}Dto();
        ${tableName!}Dto.setId(1l);
        ${tableName!}Dto.set${TableName!}Id("1");
        ${tableName!}Dto.setUserId("1");
        ${tableName!}Dto.setName("test");
        ${tableName!}Dto.setGender(0);
        ${tableName!}Dto.set${TableName!}birthExpectedDate("20200202");
        ${tableName!}Dto.setBirthDate("20200202");
        doReturn(1).when(${tableName!}Repository).update${TableName!}(any());
        doReturn(${tableName!}Dto).when(${tableName!}Repository).get${TableName!}ById(any());
        try {
            ${tableName!}Dto.setId(null);
            ${tableName!}Service.update${TableName!}(${tableName!}Dto);
        } catch (Exception e) {
            assertEquals(e.getClass(), BusinessException.class);
        }

        ${tableName!}Dto.setId(1l);

        ${TableName!}Dto ${tableName!} = ${tableName!}Service.update${TableName!}(${tableName!}Dto);
        assertNotNull(${tableName!});
    }
}
